(function(app){
	app
		.controller("content_55_inputCtrl",["$scope","$state","myFactory",'$stateParams',"$q",'$timeout','$location','$http','$ionicHistory',function($scope,$state,myFactory,$stateParams,$q,$timeout,$location,$http,$ionicHistory){
			//参数定义、接收
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var policyData;//保单详情
			var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容，比如item02_1
			var searchParam = $stateParams.searchParam;
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			var token=sessionStorage.getItem("token");
			//绑定参数到页面
			$scope.chdrnum01 = chdrnum01;
			$scope.billFlagTemp = false;//声明标志，默认为false
			$scope.contractFlagTemp = false;//声明标志，默认为false

			$scope.data = {
				checkFlag: false,
				wayVal: null,
				wayList: [
					{
						id: '0011',
						name: '经济或家庭原因'
					},
					{
						id: '0012',
						name: '过期'
					}
				],
			};

			/**********************************************初始化函数begin****************************************************/
				//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						itemData = response.data.data.modelMap;
						$scope.item55 = itemData.item55;
						var item55 = itemData.item55;

						$("#reasonSelect").val('-1');//设置为缓存中的选项
						$scope.changeReason();//执行改变下拉框方法

						var billFlag = item55.billFlag;
						//billFlag='1';
						if(billFlag=='1'){
							$("#yssmFlag").attr("checked","checked");
							$scope.invoiceCheckBox();
							console.log("billFlagTemp:"+$scope.billFlagTemp);
							console.log("billFlag:"+$scope.billFlag);
						}else{
							//$("#yssmFlag").removeAttr("checked");
							$scope.billFlag = "0";
						}

						var contractFlag = item55.contractFlag;
						if(contractFlag=='1'){
							$("#khxzFlag").attr("checked","checked");
							$scope.contractCheckBox();
							console.log("contractFlagTemp:"+$scope.contractFlagTemp);
							console.log("contractFlag:"+$scope.contractFlag);
						}else{
							$scope.contractFlag = "0";
						}

						/*var _productType = item55.productType;
						var investDiv_   = document.getElementById("investDiv55");   // 投连产品的提示
						//	存在投连产品时显示
						if( _productType.indexOf("U") > -1 ){
							//	取消隐藏
							investDiv_.style.display = '';
						}*/

						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
			/**********************************************初始化函数end****************************************************/

			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//返回上一页
			$scope.goBack = function(){
				myFactory.goBack();
			}
			//关闭遮罩
			$scope.overwindowClose = function(){
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn55Input','overwindowOn55Input','policyCloseXOn55Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn55Input = function(){
				modal('close','policyModalOn55Input','overwindowOn55Input','policyCloseXOn55Input');
			}

			//改变下拉框
			$scope.changeReason = function(){
				var options = $("#reasonSelect option:selected");
				console.log("id:"+options.val());
				console.log("val:"+options.text());
				$scope.reasonInput = options.text();
			}

			$scope.invoiceCheckBox = function(){
				$scope.billFlagTemp = !$scope.billFlagTemp;
				if($scope.billFlagTemp==true){
					$scope.billFlag = "1";
				}else{
					$scope.billFlag = "0";
				}
				console.log("billFlagTemp:"+$scope.billFlagTemp);
				console.log("billFlag:"+$scope.billFlag);
			}

			$scope.contractCheckBox = function(){
				$scope.contractFlagTemp = !$scope.contractFlagTemp;
				if($scope.contractFlagTemp==true){
					$scope.contractFlag = "1";
				}else{
					$scope.contractFlag = "0";
				}
				console.log("contractFlagTemp:"+$scope.contractFlagTemp);
				console.log("contractFlag:"+$scope.contractFlag);
			}

			//下一步
			$scope.nextStep = function(){
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}

				var options = $("#reasonSelect option:selected");
				var reason = options.val(); //选择原因
				console.log(reason);
				if(reason=="-1"){
					myFactory.alert("","请选择退保原因","info");
					return;
				}

				var billFlag = $scope.billFlag;
				var contractFlag = $scope.contractFlag;
				//console.log("billFlagTemp:"+$scope.billFlagTemp);
				console.log("billFlag:"+$scope.billFlag);
				//console.log("contractFlagTemp:"+$scope.contractFlagTemp);
				console.log("contractFlag:"+$scope.contractFlag);

				var item={
					"itemId":itemId,
					"reason":reason,
					"contractFlag":contractFlag,
					"billFlag":billFlag
				}

				$ionicHistory.clearCache().then(function(){
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy,
						searchParam:searchParam
					});
				})
			}

		}]);

		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});

})(app)